package com.aitaoxue.palx.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.viewpagerindicator.CirclePageIndicator;

public class WelcomeActivity extends Activity implements OnPageChangeListener,
		Callback, OnTouchListener {

	@InjectView(R.id.viewpager)
	ViewPager pager;
	@InjectView(R.id.indicator)
	public CirclePageIndicator indicator;

	private Integer[] ids = new Integer[] { R.drawable.welcome_img_1,
			R.drawable.welcome_img_2, R.drawable.welcome_img_3 };

	private List<View> list;

	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// setContentView(new AnimationSurfaceView(this));

		setContentView(R.layout.activity_welcome);

		ButterKnife.inject(this);

		handler = new Handler(this);
		initView();
	}

	private void initView() {
		list = new ArrayList<View>();
		ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		for (Integer id : ids) {
			ImageView imageView = new ImageView(this);
			imageView.setImageResource(id);

			imageView.setLayoutParams(lp);
			imageView.setScaleType(ScaleType.FIT_XY);

			list.add(imageView);
		}

		pager.setAdapter(new ViewPagerAdapter());
		indicator.setViewPager(pager);
		indicator.setOnPageChangeListener(this);
		pager.setOnTouchListener(this);
	}

	private int lastX;
	private int currentPosition;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			if (Math.abs(lastX - event.getX()) > 100
					&& currentPosition == (list.size() - 1)) {
				handler.sendEmptyMessage(0);
			}
			break;
		default:
			break;
		}

		return super.onTouchEvent(event);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		currentPosition = arg0;
	}

	class ViewPagerAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(list.get(position));
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(list.get(position), 0);
			return list.get(position);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

	private boolean isToLogin = false;

	@Override
	public boolean handleMessage(Message msg) {
		synchronized (this) {
			if (!isToLogin) {
				startActivity(new Intent(this, LoginActivity.class));
				finish();
				isToLogin = true;
			}
		}

		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			if (lastX - event.getX() > 100
					&& currentPosition == (list.size() - 1)) {
				handler.sendEmptyMessage(0);
			}
			break;
		default:
			break;
		}

		return false;
	}
	
	@Override
	public void onBackPressed() {
	}

}
